/* Copyright 2014 predic8 GmbH, www.predic8.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License. */
package com.predic8.membrane.annot;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

/**
 * Utility class to read the /META-INF/membrane.namespaces files generated by the annotation processor
 * ({@link SpringConfigurationXSDGeneratingAnnotationProcessor} using {@link com.predic8.membrane.annot.generator.NamespaceInfo}).
 *
 * The membrane.namespaces files describe the XML namespaces declared by the {@link MCMain} annotations.
 * These files simply help avoid having to scan all classes for the {@link MCMain} annotation.
 *
 * There may be multiple /META-INF/membrane.namespaces files residing in different jars in the classpath.
 * {@link NamespaceUtil} collects the information from all of these.
 */
public class NamespaceUtil {

	private static class NamespaceInfo {
		private final String targetNamespace, outputName, outputPackage;

		public NamespaceInfo(String targetNamespace, String outputName,
				String outputPackage) {
			this.targetNamespace = targetNamespace;
			this.outputName = outputName;
			this.outputPackage = outputPackage;
		}

		public String getTargetNamespace() {
			return targetNamespace;
		}

		public String getOutputName() {
			return outputName;
		}

		public String getOutputPackage() {
			return outputPackage;
		}
	}

	private List<NamespaceInfo> infos = new ArrayList<NamespaceInfo>();

	public NamespaceUtil() {
		try {
			Enumeration<URL> resources = getClass().getClassLoader().getResources("/META-INF/membrane.namespaces");
			while (resources.hasMoreElements()) {
				URL url = resources.nextElement();
				Properties p = new Properties();
				InputStream s = url.openStream();
				try {
					p.load(s);
				} finally {
					s.close();
				}

				int i = 1;
				while (true) {
					String key = "schema" + (i++);
					if (!p.containsKey(key + "-targetNamespace"))
						break;
					String targetNamespace = p.getProperty(key + "-targetNamespace");
					String outputName = p.getProperty(key + "-outputName");
					String outputPackage = p.getProperty(key + "-outputPackage");

					infos.add(new NamespaceInfo(targetNamespace, outputName, outputPackage));
				}
			}
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public List<String> getTargetNamespaces() {
		ArrayList<String> res = new ArrayList<String>();
		for (NamespaceInfo ni : infos)
			res.add(ni.getTargetNamespace());
		return res;
	}

	private NamespaceInfo getInfo(String targetNamespace) {
		for (NamespaceInfo ni : infos)
			if (ni.getTargetNamespace().equals(targetNamespace))
				return ni;
		throw new InvalidParameterException("targetNamespace '" + targetNamespace + "' not defined.");
	}

	public String getOutputName(String targetNamespace) {
		return getInfo(targetNamespace).getOutputName();
	}

	public String getOutputPackage(String targetNamespace) {
		return getInfo(targetNamespace).getOutputPackage();
	}
}
